#include "Headers.h"
#include "GameDefine.h"
#include "GUIButton.h"


CGUIButton::CGUIButton(void)
{
}


CGUIButton::~CGUIButton(void)
{
	this->Uninitialize();
}

bool CGUIButton::Initialize(int num, int UnSelect, int Select, int x, int y, bool selected, ButtonFunction bfn)
{
	m_UnSelectImage = LoadBitmap(g_hInst, MAKEINTRESOURCE(UnSelect));
	m_SelectImage = LoadBitmap(g_hInst, MAKEINTRESOURCE(Select));
	if(m_UnSelectImage == NULL)
		return false;
	if(m_SelectImage == NULL)
		return false;

	BITMAP bit;
	GetObject(m_UnSelectImage, sizeof(BITMAP), &bit);
	m_buttonNum = num;
	m_buttonX = x;
	m_buttonY = y;
	m_buttonWidth = bit.bmWidth;
	m_buttonHeight = bit.bmHeight;
	m_SelectedButton = selected;
	m_function = bfn;
	return true;
}

void CGUIButton::Uninitialize()
{
	DeleteObject(m_UnSelectImage);
	DeleteObject(m_SelectImage);
}

bool CGUIButton::Update(int Update, int SelectedNum)
{
	if(m_buttonNum == SelectedNum)
	{
		m_SelectedButton = true;
	}
	else
	{
		m_SelectedButton = false;
	}
	if(m_SelectedButton == true && Update == ENTERED)
	{
		m_function();
		return false;
	}
	return true;
}

void CGUIButton::Render(HDC hdc)
{
	HDC MemDC = CreateCompatibleDC(hdc);
	HBITMAP OldBitmap;

	if(m_SelectedButton)
		OldBitmap = (HBITMAP)SelectObject(MemDC, m_SelectImage);
	else
		OldBitmap = (HBITMAP)SelectObject(MemDC, m_UnSelectImage);

	TransparentBlt(hdc, m_buttonX, m_buttonY, m_buttonWidth, m_buttonHeight, 
		MemDC, 0, 0, m_buttonWidth, m_buttonHeight, RGB(255, 0, 255));

	SelectObject(MemDC, OldBitmap);
	DeleteDC(MemDC);
}